package tech.bmatch.meituan;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.bmatch.meituan.model.Dish;
import tech.bmatch.meituan.model.Merchant;
import tech.bmatch.meituan.model.SearchParam;
import tech.bmatch.meituan.service.MerchantService;
import tech.bmatch.meituan.service.impl.MerchantServiceImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Application {
    private static final Logger logger = LoggerFactory.getLogger(Application.class);
    private static MerchantService merchantService = new MerchantServiceImpl();

    public static void main(String[] args) {
        logger.info("应用启动");
        merchantService.init();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String command = null;
            try {
                command = bufferedReader.readLine();
            } catch (IOException e) {
                logger.error("error from bufferedReader: ", e);
            }
            logger.info("执行命令：" + command);
            if (command.startsWith("add")) {
                String order = command.replaceAll("add ", "");
                add(order.split(" "));
            }
            if (command.startsWith("dish")) {
                String order = command.replaceAll("dish ", "");
                addDish(order.split(" "));
            }
            if (command.startsWith("search")) {
                String order = command.replaceAll("search ", "");
                search(order);
            }
            if (command.startsWith("end")) return;
        }
    }

    private static void addDish(String[] order) {
        List<Dish> dishList = new ArrayList<>();
        int dishSize = 4;
        String merchant = order[0];
        for (int i = 0; i < (order.length - 1) / dishSize; i++) {
            Dish dish = new Dish();
            int orderID = 1 + dishSize * i;
            dish.setId(order[orderID]);
            dish.setName(order[orderID + 1]);
            dish.setPrice(Double.valueOf(order[orderID + 2]));
            dish.setAmount(Integer.valueOf(order[orderID + 3]));
            dishList.add(dish);
        }
        merchantService.addDish(merchant, dishList);
    }

    private static void add(String[] order) {
        Merchant merchant = new Merchant();
        merchant.setId(order[0]);
        merchant.setName(order[1]);
        merchant.setLon(Double.valueOf(order[2]));
        merchant.setLat(Double.valueOf(order[3]));

        merchantService.add(merchant);
    }

    /**
     * 查询商家及菜品
     *
     * @param order
     */
    private static void search(String order) {
        SearchParam searchParam = new SearchParam();
        searchParam.setName(order);
        searchParam.setLon(120.026208);
        searchParam.setLat(30.279212);
        List<Merchant> merchantList = merchantService.search(searchParam);
        for (Merchant merchant : merchantList) {
            List<Dish> list = merchant.getDishes();
            System.out.println("商家名称：" + merchant.getName());
            System.out.println("两点距离为：" + merchant.getDistance() + "米");
            System.out.println("菜品：");
            if (list == null) System.out.println("无菜品信息");
            else {
                for (Dish dish : list) {
                    System.out.println("菜品名称：" + dish.getName());
                    System.out.println("菜品单价：" + dish.getPrice() + "元");
                    System.out.println("菜品数目：" + dish.getAmount() + "\n");

                }
            }
            System.out.println("----------------------------------------");
        }
    }

}
